package org.jupiter.blog.web.controller;

import org.jupiter.blog.api.dto.JupiterCategoryDTO;
import org.jupiter.blog.api.http.JupiterResult;
import org.jupiter.blog.service.service.JupiterCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/categories")
public class JupiterCategoryController {

	@Autowired
	private JupiterCategoryService categoryService;

	@GetMapping("/{uuid}")
	public JupiterResult<JupiterCategoryDTO> getById(@PathVariable String uuid) {
		JupiterCategoryDTO category = categoryService.getById(uuid);
		return JupiterResult.success(category);
	}

	@GetMapping
	public JupiterResult<List<JupiterCategoryDTO>> getAll() {
		List<JupiterCategoryDTO> categories = categoryService.getAll();
		return JupiterResult.success(categories);
	}

	@GetMapping("/page")
	public JupiterResult<Object> getByPage(
			@RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "10") int size,
			@RequestParam(required = false) String name) {

		//		List<JupiterCategoryDTO> categories = categoryService.getByPage(page, size, name);
		//		long total = categoryService.getCount(name);

		return JupiterResult.success();
	}

	@PostMapping
	public JupiterResult<Void> create(@RequestBody JupiterCategoryDTO category) {
		categoryService.create(category);
		return JupiterResult.success();
	}

	@PutMapping("/{uuid}")
	public JupiterResult<Void> update(@PathVariable String uuid, @RequestBody JupiterCategoryDTO category) {
		category.setUuid(uuid);
		categoryService.update(category);
		return JupiterResult.success();
	}

	@DeleteMapping("/{uuid}")
	public JupiterResult<Void> delete(@PathVariable String uuid) {
		categoryService.delete(uuid);
		return JupiterResult.success();
	}

	@PutMapping("/{uuid}/sort")
	public JupiterResult<Void> updateSort(@PathVariable String uuid, @RequestParam Integer sort) {
		categoryService.updateSort(uuid, sort);
		return JupiterResult.success();
	}

	@GetMapping("/active")
	public JupiterResult<List<JupiterCategoryDTO>> getActiveCategories() {
		List<JupiterCategoryDTO> categories = categoryService.getActiveCategories();
		return JupiterResult.success(categories);
	}
}
